package com.domain.cumanager;

import java.io.IOException;
import java.net.Socket;
import java.util.Hashtable;

import org.apache.log4j.Logger;

import com.domain.exceptions.NotFoundAvailableCU;

public class CUStubsHolder {
	private Hashtable<String, CUStub> m_cuStubs;
	private static Logger s_logger;
	private CUStubGarbageCollector m_garbageCollector;
	
	public CUStubsHolder(){
		s_logger = Logger.getLogger(CUStubsHolder.class);		
		m_cuStubs = new Hashtable<String, CUStub>();
		m_garbageCollector = new CUStubGarbageCollector(m_cuStubs);
		m_garbageCollector.start();
	}

	//TODO handle all possible exceptions
	public void addCUStub(Socket clientSocket) {
		CUStub cuStub;
		try {
			cuStub = new CUStub(clientSocket);
			String cuStubId = cuStub.getCUId();
			m_cuStubs.put(cuStubId, cuStub);
			cuStub.start();			
		} catch (IOException e) {
			s_logger.fatal("Failed to initialize CUStub");
			try {
				clientSocket.close();
			} catch (IOException e1) {
				s_logger.fatal("Failed to close CUStubs socket");
			}
		}
	}

	public CUStub getAvailableCU() throws NotFoundAvailableCU{
		// TODO Auto-generated method stub
		throw new NotFoundAvailableCU();
	}

}
